package com.dsbj.mp.icardservice.service.mcommon;

import com.dsbj.mp.icardservice.entity.Employee;
import com.dsbj.mp.icardservice.entity.Persont;
import com.dsbj.mp.icardservice.service.attendance.MonthReportService;
import com.dsbj.mp.icardservice.utils.entity.EmployeeCommon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author cjw 2020-01-21
 */
@Service
@Transactional
public class McommonService {
	
	public static Logger logger = LoggerFactory.getLogger(McommonService.class);
	
	
	
	/**
	 * 从列表中取一项，若该列表存在多条记录，抛出异常。
	 * @param list_t
	 * @param <T>
	 * @return
	 * @author cjw 2019-12-31
	 */
	public static <T> T getOneFromList(List<T> list_t){
		if(list_t==null||list_t.isEmpty()){
			return null;
		}
		if(list_t.size()>1){
			logger.error("list_t中含有多条记录!className="+list_t.get(0).getClass().getName());
			throw new RuntimeException("list_t中含有多条记录!");
		}
		return list_t.get(0);
	}
	
	/**
	 * 合并list_employee,list_persont中的员工信息,并去除重复的员工信息。
	 * @param list_employee
	 * @param list_persont
	 * @return
	 */
	public static List<EmployeeCommon> getEmployeeCommon_fromEmployeeAndPersont(List<Employee> list_employee, List<Persont> list_persont){
		if(list_employee==null && list_persont==null){
			return null;
		}
		
		/**
		 * 将list_employee,list_persont中的信息存入list_employeeCommon中。
		 */
		List<EmployeeCommon> list_employeeCommon=new ArrayList<>();
		EmployeeCommon employeeCommon=null;
		
		//将list_employee中的信息存入list_employeeCommon中。
		if(list_employee!=null){
			for(Employee employee: list_employee){
				if(employee==null){
					continue;
				}
				employeeCommon=new EmployeeCommon();
				employeeCommon.setIdcardno(employee.getIdcardno());
				employeeCommon.setCompanyno(employee.getCompanyno());
				employeeCommon.setEmployeeno(employee.getEmployeeno());
				employeeCommon.setEmployeename(employee.getEmployeename());
				list_employeeCommon.add(employeeCommon);
			}
		}
		
		//将list_persont中的信息存入list_employeeCommon中。
		if(list_persont!=null){
			for(Persont persont: list_persont){
				if(persont==null){
					continue;
				}
				employeeCommon=new EmployeeCommon();
				employeeCommon.setIdcardno(persont.getIdcardno());
				employeeCommon.setCompanyno(persont.getCompanyno());
				employeeCommon.setEmployeeno(persont.getEmployeeno());
				employeeCommon.setEmployeename(persont.getEmployeename());
				list_employeeCommon.add(employeeCommon);
			}
		}
		
		/**
		 * 去重list_employeeCommon中的重复信息,此处根据idcardno判断员工是否重复。
		 */
		List<EmployeeCommon> list_employeeCommon_washed=new ArrayList<>();
		//员工身份证号HashMap,键值对的格式为《"idcardno=idcardno", null》
		HashMap<String,Object> map_idcardno=new HashMap<>();
		
		for(int i=0; i<list_employeeCommon.size();i++){
			employeeCommon=list_employeeCommon.get(i);
			String mapKey="idcardno="+employeeCommon.getIdcardno();
			//若map_idcardno中存在该mapKey
			if(map_idcardno.containsKey(mapKey)){
				continue;
			}else{
				//若map_idcardno中不存在该mapKey
				list_employeeCommon_washed.add(employeeCommon);
				map_idcardno.put(mapKey,null);
			}
		}
		return list_employeeCommon_washed;
	}
	
	
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2020-02-25
	 */
	public static void main(String[] args){
	
	}
}
